<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue 3 响应式原理与渲染流程可视化</title>
    <style>
      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
      }
      body {
        font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
        line-height: 1.6;
        color: #333;
        background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        min-height: 100vh;
        padding: 20px;
      }
      .container {
        max-width: 1200px;
        margin: 0 auto;
      }
      header {
        text-align: center;
        margin-bottom: 30px;
        padding: 20px;
        background: rgba(255, 255, 255, 0.9);
        border-radius: 10px;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
      }
      h1 {
        color: #42b883;
        margin-bottom: 10px;
      }
      .subtitle {
        color: #34495e;
        font-size: 1.2rem;
      }
      .main-content {
        display: flex;
        gap: 20px;
        flex-wrap: wrap;
      }
      .panel {
        flex: 1;
        min-width: 300px;
        background: white;
        border-radius: 10px;
        padding: 20px;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        margin-bottom: 20px;
      }
      .panel-title {
        color: #42b883;
        border-bottom: 2px solid #42b883;
        padding-bottom: 10px;
        margin-bottom: 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      .reactive-system {
        min-height: 400px;
      }
      .reactive-object {
        margin-bottom: 30px;
      }
      .reactive-property {
        display: flex;
        align-items: center;
        margin: 10px 0;
        padding: 10px;
        background: #f8f9fa;
        border-radius: 5px;
        transition: all 0.3s ease;
      }
      .property-name {
        font-weight: bold;
        width: 100px;
      }
      .property-value {
        flex: 1;
        padding: 8px;
        background: #e9ecef;
        border-radius: 4px;
        min-height: 38px;
      }
      .dependency {
        margin: 15px 0;
        padding: 10px;
        background: #e8f5e9;
        border-radius: 5px;
        border-left: 4px solid #66bb6a;
      }
      .effect {
        margin: 15px 0;
        padding: 10px;
        background: #e3f2fd;
        border-radius: 5px;
        border-left: 4px solid #42a5f5;
      }
      .render-process {
        min-height: 400px;
      }
      .virtual-dom,
      .real-dom {
        margin: 20px 0;
        padding: 15px;
        background: #f5f5f5;
        border-radius: 5px;
      }
      .dom-node {
        margin: 10px 0;
        padding: 10px;
        background: #fff;
        border: 1px solid #ddd;
        border-radius: 4px;
        position: relative;
      }
      .dom-node::before {
        content: "";
        position: absolute;
        left: -15px;
        top: 15px;
        width: 15px;
        height: 2px;
        background: #ddd;
      }
      .dom-children {
        margin-left: 30px;
      }
      .highlight {
        animation: highlight 1.5s ease;
      }
      @keyframes highlight {
        0% {
          background-color: #fff;
        }
        50% {
          background-color: #fff59d;
        }
        100% {
          background-color: #fff;
        }
      }
      .update-animation {
        animation: update 1s ease;
      }
      @keyframes update {
        0% {
          background-color: #fff;
        }
        50% {
          background-color: #a5d6a7;
        }
        100% {
          background-color: #fff;
        }
      }
      .controls {
        display: flex;
        gap: 10px;
        flex-wrap: wrap;
        margin-top: 20px;
      }
      button {
        padding: 10px 15px;
        background: #42b883;
        color: white;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        transition: background 0.3s;
      }
      button:hover {
        background: #359f72;
      }
      .connection-line {
        position: relative;
        height: 30px;
        margin: 10px 0;
      }
      .connection-line::before {
        content: "";
        position: absolute;
        top: 50%;
        left: 50%;
        width: 80%;
        height: 2px;
        background: #42b883;
        transform: translate(-50%, -50%);
      }
      .connection-line::after {
        content: "➤";
        position: absolute;
        top: 50%;
        right: 10%;
        transform: translateY(-50%);
        color: #42b883;
      }
      .animation-controls {
        display: flex;
        justify-content: center;
        gap: 10px;
        margin-top: 20px;
      }
      .speed-control {
        display: flex;
        align-items: center;
        gap: 10px;
      }
      .pulse {
        animation: pulse 2s infinite;
      }
      @keyframes pulse {
        0% {
          transform: scale(1);
        }
        50% {
          transform: scale(1.05);
        }
        100% {
          transform: scale(1);
        }
      }
      @media (max-width: 768px) {
        .main-content {
          flex-direction: column;
        }
      }
    </style>
  </head>
  <body>
    <div class="container">
      <header>
        <h1>Vue 3 响应式原理与渲染流程可视化</h1>
        <p class="subtitle">从响应式数据变化到DOM更新的完整过程</p>
      </header>
      <div class="panel">
        <div class="panel-title">
          <h2>交互演示</h2>
          <span>Interactive Demo</span>
        </div>

        <div class="controls">
          <button id="change-message">更改 message</button>
          <button id="increment-count">增加 count</button>
          <button id="reset">重置演示</button>
        </div>

        <div class="animation-controls">
          <div class="speed-control">
            <label for="animation-speed">动画速度:</label>
            <input
              type="range"
              id="animation-speed"
              min="0.5"
              max="2"
              step="0.5"
              value="1"
            />
            <span id="speed-value">1x</span>
          </div>
        </div>
      </div>
      <div class="main-content">
        <div class="panel reactive-system">
          <div class="panel-title">
            <h2>响应式系统</h2>
            <span>Reactive System</span>
          </div>

          <div class="reactive-object">
            <h3>响应式对象</h3>
            <div class="reactive-property">
              <span class="property-name">message:</span>
              <span class="property-value" id="message-value"
                >"Hello Vue 3!"</span
              >
            </div>
            <div class="reactive-property">
              <span class="property-name">count:</span>
              <span class="property-value" id="count-value">0</span>
            </div>
          </div>

          <div class="dependency">
            <h4>依赖收集 (Deps)</h4>
            <div>渲染函数 (Render Function)</div>
            <div>计算属性 (Computed Properties)</div>
            <div>侦听器 (Watchers)</div>
          </div>

          <div class="effect">
            <h4>副作用 (Effects)</h4>
            <div>组件渲染</div>
            <div>计算属性更新</div>
            <div>侦听器回调</div>
          </div>
        </div>

        <div class="panel render-process">
          <div class="panel-title">
            <h2>渲染流程</h2>
            <span>Rendering Process</span>
          </div>

          <div class="virtual-dom">
            <h3>虚拟DOM (Virtual DOM)</h3>
            <div class="dom-node">
              div (root)
              <div class="dom-children">
                <div class="dom-node">
                  h1
                  <div class="dom-children">
                    <div class="dom-node">"Hello Vue 3!"</div>
                  </div>
                </div>
                <div class="dom-node">
                  button
                  <div class="dom-children">
                    <div class="dom-node">"Count: 0"</div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <div class="connection-line"></div> 

          <div class="real-dom">
            <h3>真实DOM (Real DOM)</h3>
            <div class="dom-node">
              div (root)
              <div class="dom-children">
                <div class="dom-node">
                  h1
                  <div class="dom-children">
                    <div class="dom-node" id="real-dom-message">
                      "Hello Vue 3!"
                    </div>
                  </div>
                </div>
                <div class="dom-node">
                  button
                  <div class="dom-children">
                    <div class="dom-node" id="real-dom-count">"Count: 0"</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <script>
      document.addEventListener("DOMContentLoaded", function () {
        // 状态管理
        const state = {
          message: "Hello Vue 3!",
          count: 0,
          animationSpeed: 1,
        };

        // DOM 元素引用
        const messageValueEl = document.getElementById("message-value");
        const countValueEl = document.getElementById("count-value");
        const realDomMessageEl = document.getElementById("real-dom-message");
        const realDomCountEl = document.getElementById("real-dom-count");
        const changeMessageBtn = document.getElementById("change-message");
        const incrementCountBtn = document.getElementById("increment-count");
        const resetBtn = document.getElementById("reset");
        const animationSpeedSlider = document.getElementById("animation-speed");
        const speedValueEl = document.getElementById("speed-value");

        // 初始化
        updateUI();

        // 事件监听
        changeMessageBtn.addEventListener("click", changeMessage);
        incrementCountBtn.addEventListener("click", incrementCount);
        resetBtn.addEventListener("click", resetDemo);
        animationSpeedSlider.addEventListener("input", updateAnimationSpeed);

        // 更新动画速度
        function updateAnimationSpeed() {
          state.animationSpeed = parseFloat(animationSpeedSlider.value);
          speedValueEl.textContent = state.animationSpeed + "x";
        }

        // 更改消息
        function changeMessage() {
          // 模拟响应式数据更新
          highlightElement(
            ".reactive-property:first-child .property-value",
            () => {
              state.message = "Vue 3 is awesome!";
              updateUI();

              // 模拟依赖追踪和更新
              highlightElement(".dependency", () => {
                highlightElement(".effect", () => {
                  // 模拟虚拟DOM更新
                  highlightElement(".virtual-dom", () => {
                    // 模拟真实DOM更新
                    highlightElement(".real-dom", () => {
                      // 完成更新
                      realDomMessageEl.textContent = `"${state.message}"`;
                      realDomMessageEl.classList.add("update-animation");
                      setTimeout(() => {
                        realDomMessageEl.classList.remove("update-animation");
                      }, 1000 / state.animationSpeed);
                    });
                  });
                });
              });
            }
          );
        }

        // 增加计数
        function incrementCount() {
          // 模拟响应式数据更新
          highlightElement(
            ".reactive-property:nth-child(2) .property-value",
            () => {
              state.count++;
              updateUI();

              // 模拟依赖追踪和更新
              highlightElement(".dependency", () => {
                highlightElement(".effect", () => {
                  // 模拟虚拟DOM更新
                  highlightElement(".virtual-dom", () => {
                    // 模拟真实DOM更新
                    highlightElement(".real-dom", () => {
                      // 完成更新
                      realDomCountEl.textContent = `"Count: ${state.count}"`;
                      realDomCountEl.classList.add("update-animation");
                      setTimeout(() => {
                        realDomCountEl.classList.remove("update-animation");
                      }, 1000 / state.animationSpeed);
                    });
                  });
                });
              });
            }
          );
        }

        // 重置演示
        function resetDemo() {
          state.message = "Hello Vue 3!";
          state.count = 0;
          updateUI();

          // 添加一些视觉反馈
          document
            .querySelectorAll(".reactive-property .property-value")
            .forEach((el) => {
              el.classList.add("highlight");
              setTimeout(() => {
                el.classList.remove("highlight");
              }, 1000);
            });
        }

        // 更新UI反映当前状态
        function updateUI() {
          messageValueEl.textContent = `"${state.message}"`;
          countValueEl.textContent = state.count;
        }

        // 高亮元素辅助函数
        function highlightElement(selector, callback) {
          const element = document.querySelector(selector);
          if (element) {
            element.classList.add("highlight");
            setTimeout(() => {
              element.classList.remove("highlight");
              if (callback) {
                setTimeout(callback, 500 / state.animationSpeed);
              }
            }, 1000 / state.animationSpeed);
          } else if (callback) {
            callback();
          }
        }
      });
    </script>
  </body>
</html>
